package com.longrise.android;

import android.app.Application;

import com.longrise.LEAP.BO.Extend.leapcodevalue;
import com.longrise.LEAP.Base.Objects.UserInfo;
import com.longrise.LEAP.Base.Service.LEAPServiceClient;
import com.longrise.LEAP.Base.Util.ProcessHandler;

import java.io.File;
import java.util.Hashtable;

public class Global extends Application
{
	//全局事件说明LSMsgCall(int type, Object... parameters){
	/*
	 * 以下为已经被占用数值，各业务模块不可再使用，建议各业务模块使用正整数作为事件值
	 	type == GlobalEventCode.FormClose : 窗口关闭事件 
	 	type == GlobalEventCode.Message ：短信事件
	 	type == GlobalEventCode.Camera: 相机事件
	 	type == GlobalEventCode.Eben_Pdf:	ERenEBen pdf
	 	type == GlobalEventCode.ChooserFile:	文件选择器
	 */
	//全局事件说明}
	
	// 通用属性定义,不可变更lilw{
	private static Global _instance = null;
	private boolean _isdebugger = true;
	private String _appName = null;
	private String _userflag = null;
	private String _pwd = null;
	private String _defaultServerUrl = null;
	private String _updateUrl = null;
	private String _versionName = "V1.0";
	private int _versionCode = -1;
	private String _leapAppName = "BAOA"; //leap平台中，对当前App管理的名字，用于版本更新模块
	private String _appCode = null;// 用于leap登录服务
	private String _appId = null;// 用于leap登录服务
	private String _appValue = null;// 用于leap登录服务
	private String _appArea = null;
	private String _systemName = null;
	private int _fontType = 0;//字体类型 0：android默认，1：楷体，2：宋体
	//private List<listMenuInfo> _meuns = null;
	private String _welcomeString = null;//欢迎语
	private String _sharedPreferencesName = "baoaInfo";//本地用户操作习惯数据缓存名
	private UserInfo _userinfo = null;
	private int _msgTask = 0;//消息提醒间隔（访问服务时间间隔）
	private Hashtable<String,leapcodevalue[]> _codeValue = null; //代码表
	private String _defaultServiceName = "leap";
	private boolean _isHaveNet = true;
	// 通用属性定义}

	@Override
	public void onCreate() {
		super.onCreate();
		Global.getInstance();
	}
	// 业务属性定义，将根据业务需求变更{

	// 业务属性定义，将根据业务需求变更}

	public void setServiceName(String name)
	{
		this._defaultServiceName = name;
	}
	
	public String getServiceName()
	{
		return this._defaultServiceName;
	}
	
	public boolean isDebugger()
	{
		return this._isdebugger;
	}
	public void setDebugger(boolean b)
	{
		this._isdebugger = b;
	}
	
	public boolean _isHaveNet() 
	{
		return _isHaveNet;
	}

	public void setHaveNet(boolean _isHaveNet) 
	{
		this._isHaveNet = _isHaveNet;
	}
	
	public Hashtable<String,leapcodevalue[]> getCodeValue()
	{
		if(null==this._codeValue)
		{
			this._codeValue = new Hashtable<String, leapcodevalue[]>();
		}
		return this._codeValue;
	}
	
	public void setMsgTask(int i)
	{
		this._msgTask = i;
	}
	public int getMsgTask()
	{
		return this._msgTask;
	}
		
	public void addClient(String clientName, String serviceName, String serverUrl)
	{
		FrameworkManager.getInstance().addClient(clientName, serviceName, serverUrl);
	}
	
	public void removeClient(String clientName)
	{
		FrameworkManager.getInstance().removeClient(clientName);
	}
	
	public LEAPServiceClient getClient()
	{
		FrameworkManager.getInstance().addClient(this._defaultServiceName, this._defaultServiceName, this._defaultServerUrl);
		FrameworkManager.getInstance().setDefaultServiceName(this._defaultServiceName);
		return FrameworkManager.getInstance().getClient();
	}
	
	public LEAPServiceClient getClient(String clientName)
	{
		return FrameworkManager.getInstance().getClient(clientName);
	}	
	
	public void setPwd(String p)
	{
		this._pwd = p;
	}
	public String getPwd()
	{
		return this._pwd;
	}
	public void setUserInfo(UserInfo uinfo)
	{
		this._userinfo = uinfo;
	}
	public UserInfo getUserInfo()
	{
		return this._userinfo;
	}
		
	@Deprecated
	public void setSPName(String spname)
	{
		this._sharedPreferencesName = spname;
	}
	
	@Deprecated
	public String getSPName()
	{
		return this._sharedPreferencesName;
	}
	@Deprecated
	public void setWelcomeString(String welcomeString)
	{
		this._welcomeString = welcomeString;
	}
	@Deprecated
	public String getWelcomeString()
	{
		return this._welcomeString;
	}
	
	/**
	 * leap平台中，对当前App管理的名字，用于版本更新模块
	 * @param name
	 */
	public void setLeapAppName(String name)
	{
		this._leapAppName = name;
	}
	
	public String getLeapAppName()
	{
		return this._leapAppName;
	}
	/**
	 * 字体类型（0：android默认，1：楷体，2：宋体）
	 * @return
	 */
	@Deprecated
	public int getFontType()
	{
		return this._fontType;
	}
	@Deprecated
	public void setFontType(int fontType)
	{
		this._fontType = fontType;
	}
	/**
	 * 当前版本号
	 * 
	 * @return
	 */
	public int getVersionCode()
	{
		return _versionCode;
	}
	public void setVersionCode(int versionCode)
	{
		this._versionCode = versionCode;
	}

	
	public String getVersionName()
	{
		return _versionName;
	}
	/**
	 * 当前版本名
	 * 
	 * @return
	 */
	public void setVersionName(String vname)
	{
		this._versionName = vname;
	}

	public String getAppCode()
	{
		return _appCode;
	}

	public void setAppCode(String _appCode)
	{
		this._appCode = _appCode;
	}

	public String getAppId()
	{
		return _appId;
	}

	public void setAppId(String _appId)
	{
		this._appId = _appId;
	}

	public String getAppValue()
	{
		return _appValue;
	}

	public void setAppValue(String _appValue)
	{
		this._appValue = _appValue;
	}
	public String getAppArea()
	{
		return _appArea;
	}

	public void setAppArea(String appArea)
	{
		this._appArea = appArea;
	}
	public String getSystemName()
	{
		return _systemName;
	}

	public void setSystemName(String systemName)
	{
		this._systemName = systemName;
	}
	/**
	 * 升级路径
	 * 
	 * @return
	 */
	public String getUpdateUrl()
	{
		return _updateUrl;
	}

	public String getServerUrl(String clientName)
	{
		return FrameworkManager.getInstance().getServerUrl(clientName);
	}
	
	/**
	 * 升级路径
	 * 
	 * @return
	 */
	public void setUpdateUrl(String url)
	{
		this._updateUrl = url;
	}

	/**
	 * leap服务路径
	 * 
	 * @return
	 */
	public String getServerUrl()
	{
		return _defaultServerUrl;
	}

	/**
	 * leap服务路径
	 * 
	 * @return
	 */
	public void setServerUrl(String url)
	{
		if (null != url && !"".equals(url))
		{
			if (url.lastIndexOf("/") != url.length() - 1)
			{
				url += "/";
			}
		}

		this._defaultServerUrl = url;
	}

	/**
	 * 获取当前登录用户
	 * 
	 * @return
	 */
	public String getUserflag()
	{
		return _userflag;
	}

	/**
	 * 设置当前登录用户，只能在登录模块设置
	 * 
	 * @param _userflag
	 */
	public void setUserflag(String _userflag)
	{
		this._userflag = _userflag;
	}
	
	public void setAppName(String name)
	{
		this._appName = name;
	}
	
	public String getAppName()
	{
		return this._appName;
	}

	/**
	 * 允许在UI线程中调用服务 此功能不开放，所有服务调用必须在后台线程中执行 by lilw
	 */
	/*
	 * public void setWebServer() { try {
	 * if(android.os.Build.VERSION.SDK_INT>10) { StrictMode.setThreadPolicy(new
	 * StrictMode
	 * .ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork
	 * ().penaltyLog().build()); StrictMode.setVmPolicy(new
	 * StrictMode.VmPolicy.Builder
	 * ().detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build()); } }
	 * catch (Exception e) { } }
	 */

	public Global()
	{
//		if(null==this._meuns)
//		{
//			this._meuns = new ArrayList<listMenuInfo>();
//		}
	}

	public static Global getInstance()
	{
		if (null == _instance)
		{
			synchronized (Global.class) 
			{
				if (null == _instance)
				{
					_instance = new Global();
				}
			}
		}
		
		return _instance;
	}

	
	public Object call(String server, Class<?> classType, Object... parameters)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient();
			if(null!=client)
			{
				return client.call(server, classType, parameters);
			}
		}
		catch (Exception ex){}
		finally
		{
			server = null;
			classType = null;
			parameters = null;
			client = null;
		}
		return null;
	}
	
	public Object call(int connTimeout, int soTimeout, String server, Class<?> classType, Object... parameters)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient();
			if(null!=client)
			{
				return client.call(connTimeout, soTimeout, server, classType, parameters);
			}
		}
		catch (Exception ex){}
		finally
		{
			server = null;
			classType = null;
			parameters = null;
			client = null;
		}
		return null;
	}
	
	public Object call(String serviceName, String server, Class<?> classType, Object... parameters)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient();
			if(null!=client)
			{
				return client.call(serviceName, server, classType, parameters);
			}
		}
		catch (Exception ex){}
		finally
		{
			server = null;
			classType = null;
			parameters = null;
			client = null;
		}
		return null;
	}
	
	public Object call(int connTimeout, int soTimeout, String serviceName, String server, Class<?> classType, Object... parameters)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient();
			if(null!=client)
			{
				return client.call(connTimeout, soTimeout, serviceName, server, classType, parameters);
			}
		}
		catch (Exception ex){}
		finally
		{
			server = null;
			classType = null;
			parameters = null;
			client = null;
		}
		return null;
	}
	
	public byte[] download(String url)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient();
			if(null!=client)
			{
				return client.download(url);
			}
		}
		catch (Exception ex){}
		finally
		{
			url = null;
			client = null;
		}
		return null;
	}
	
	public byte[] download(String serviceName, String url)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient(serviceName);
			if(null!=client)
			{
				return client.download(url);
			}
		}
		catch (Exception ex){}
		finally
		{
			url = null;
			client = null;
		}
		return null;
	}
	
	public boolean download(String url, File file, ProcessHandler handler)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient();
			if(null!=client)
			{
				return client.download(url, file, handler);
			}
		}
		catch (Exception ex){}
		finally
		{
			url = null;
			client = null;
		}
		return false;
	}
	
	public boolean download(String serviceName, String url, File file, ProcessHandler handler)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient(serviceName);
			if(null!=client)
			{
				return client.download(url, file, handler);
			}
		}
		catch (Exception ex){}
		finally
		{
			url = null;
			client = null;
		}
		return false;
	}
	
	
	public Object callExternal(String clientName, String server, Class<?> classType, Object... parameters)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient(clientName);
			if(null!=client)
			{
				return client.call(server, classType, parameters);
			}
		}
		catch (Exception ex){}
		finally
		{
			server = null;
			classType = null;
			parameters = null;
			client = null;
			clientName = null;
		}
		return null;
	}
	
	public Object callExternal(int connTimeout, int soTimeout, String clientName, String server, Class<?> classType, Object... parameters)
	{
		LEAPServiceClient client = null;
		try
		{
			client = getClient(clientName);
			if(null!=client)
			{
				return client.call(connTimeout, soTimeout, server, classType, parameters);
			}
		}
		catch (Exception ex){}
		finally
		{
			server = null;
			classType = null;
			parameters = null;
			client = null;
			clientName = null;
		}
		return null;
	}
	
	
	
	public void clean()
	{
		FrameworkManager.getInstance().restClient();		
	}
	


	/**
	 * 通过服务调用获取当前用户的菜单数据
	 * @return
	 */
//	public List<listMenuInfo> getMenus()
//	{
//		return this._meuns;
//	}
	
//	public void addMenu(listMenuInfo menu)
//	{
//		if(null==this._meuns)return;
//		this._meuns.add(menu);
//	}
	public void cleanMeun()
	{
//		if(null!=this._meuns)
//		{
//			this._meuns.clear();
//		}
	}
	
	public void onDistory()
	{		
		_codeValue = null;
//		if(null!=_meuns)
//		{
//			_meuns.clear();
//		}
//		_meuns = null;
		_userflag = null;
		_defaultServerUrl = null;
		_defaultServiceName = null;
		_updateUrl = null;
		_versionName = null;
		_appCode = null;
		_appId = null;
		_appValue = null;
		_instance = null;
		_userinfo = null;
	}
}
